package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.ProductBean;
import com.yanchangapp.model.bean.WareHouseBean;
import com.yanchangapp.model.cache.UserCache;
import com.yanchangapp.model.request.CreateOrderRequest;
import com.yanchangapp.model.response.CreateOrderResponse;
import com.yanchangapp.ui.view.POrderCtCntAtView;
import com.zxl.baselib.bean.response.BaseListResponse;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.zxl.baselib.util.time.TimeUtil;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-08-07.
 * description:
 *    填写采购明细P层 --- 从合同进入
 */
public class POrderCtCntAtPresenter extends BasePresenter<POrderCtCntAtView> {

    public POrderCtCntAtPresenter(BaseActivity context) {
        super(context);
    }

    /**
     * 加载仓库地址的数据
     */
    public void loadWareHouseData(){
        HeadApiRetrofit.getInstance().queryAllWareHouse()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseListResponse<WareHouseBean>>() {
                    @Override
                    public void onNext(BaseListResponse<WareHouseBean> wareHouseBeanBaseResponse) {
                        if (AppConstant.REQUEST_SUCCESS == wareHouseBeanBaseResponse.getCode()){
                            if (getView() != null){
                                getView().queryWareHouseSuccess(wareHouseBeanBaseResponse.getData());
                            }
                        }else {
                            if (getView() != null){
                                getView().queryWareHouseFailure(wareHouseBeanBaseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().queryWareHouseFailure(exception.getErrorInfo());
                        }
                    }
                });
    }


    public void actionCreateOrder(String contactId,
                                  String supplierId,
                                  String storeRoom,
                                  double totalMoney,
                                  List<ProductBean> mProductsList){
        if (getView() != null){
            getView().showLoadingDialog();
        }
        CreateOrderRequest createOrderRequest = new CreateOrderRequest(
                TimeUtil.getCurrentTime(),
                UserCache.getDepartmentId(),
                contactId,
                UserCache.getEmployeeId(),
                UserCache.getName(),
                supplierId,
                storeRoom,
                totalMoney,
                mProductsList);
        HeadApiRetrofit.getInstance().createOrder(createOrderRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<CreateOrderResponse>() {
                    @Override
                    public void onNext(CreateOrderResponse response) {
                        if (AppConstant.REQUEST_SUCCESS == response.getCode()){
                            if (getView() != null){
                                getView().hideLoadingDialog();
                                getView().createOrderSuccess(response.getData());
                            }
                        }else {
                            if (getView() != null){
                                getView().hideLoadingDialog();
                                getView().createOrderFailure(response.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            getView().createOrderFailure("");
                        }
                    }
                });
    }
}
